<template>
  <div>
    <el-tabs
      class="customer-tab"
      type="card"
      @tab-click="jump"
      v-model="tabName"
    >
      <el-tab-pane
        v-for="(tab, index) in proCla"
        :name="tab.refName"
        :key="index"
        :label="tab.name"
      >
        <div class="scroll-content" @scroll="onScroll" :style=" 'overflow-x: hidden; overflow-y: auto;height:' + contentStyleObj.height ">
          <div :ref="tab.refName" class="scroll-item">
            <div class="line-name">
              <h2>{{ tab.name }}</h2>
            </div>
            <div>
              <div style="height: 40px" v-for="(item,index1) in tab" :key="index1">
                <div>
                  {{item.toString()}}
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>

  </div>
</template>

<script>
export default {
  name: 'oldProDetail',
  props: {
    proDetail: {
      type: Object
      // default() {
      //   return {}
      // }
    }
  },
  components: {},
  data() {
    return {
      tabIndex: '0',
      proCla: {
        basicInfo: [],
        engineInfo: [],
        investmentInfo: [],
        constructionInfo: []
      },
      contentStyleObj: {
        height: '100px'
      },
      tabName: '基本信息',

      ench: [{ 'xmbh': '项目编号' },
        { 'xmmc': '项目名称' },
        { 'xmzgbm': '项目主管部门' },
        { 'gbzntjsyear': '高标准农田建设任务所属年度' },
        { 'xmsheng': '项目所在省' },
        { 'xmshi': '项目所在市' },
        { 'xmxian': '项目所在县' },
        { 'beizhu': '备注' },

        { 'yswjmc': '验收文件名称' },
        { 'yswjwh': '验收文件文号' },
        { 'ysrq': '验收日期' },
        { 'jsddys': '建设地点（验收）' },
        { 'xmqgdzbys': '项目区拐点坐标（验收）' },
        { 'gbzntgdzb': '高标准农田拐点坐标（验收）' },

        { 'ztzys': '总投资（验收）' },
        { 'shtzys': '社会投资（验收）' },
        { 'zytzys': '中央投资（验收）' },
        { 'dftzys': '地方投资（验收）' },

        { 'jsgmys': '建设规模（验收）' },
        { 'gbzntjcmjys': '高标准农田建成面积（验收）' },
        { 'xzgdmjys': '新增耕地面积（验收）' },
        { 'xzgxjsggmj': '新增高效节水灌溉面积' },
        { 'jcgnzntzldj': '建成高标准农田质量等级' },

      ],
      infoClaity:{
        basicInfo:["xmbh","xmmc","xmzgbm","gbzntjsyear","xmsheng","xmshi","xmxian"],
        engineInfo:["yswjmc","yswjwh","ysrq","jsddys","xmqgdzbys","gbzntgdzb","beizhu"],
        investmentInfo:["ztzys","shtzys","zytzys","dftzys"],
        constructionInfo:["jsgmys","gbzntjcmjys","xzgdmjys","xzgxjsggmj","jcgnzntzldj"],
  },
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getHight()
    window.addEventListener('resizeDetail', this.getHight)
   // this.getCurrentProDetails(this.proDetail)
  },
mounted() {
 // this.getCurrentProDetails(this.proDetail)
},
  destroyed() {
    window.removeEventListener('resizeDetail', this.getHight)
  },
  methods: {
    getCurrentProDetails(curpro) {
      this.proCla.basicInfo = { }
      this.proCla.engineInfo ={ }
      this.proCla.investmentInfo ={ }
      this.proCla.constructionInfo ={ }
      //2022年6月10日 LQ 哎！ 给对象赋值用了我两个小时才搞明白
      for(let i in this.infoClaity["basicInfo"]){
        let fcontext=this.infoClaity["basicInfo"][i];
        this.proCla.basicInfo["name"]="基本信息";
        this.proCla.basicInfo["refName"]="基本信息";
        this.proCla.basicInfo[fcontext]=curpro[fcontext];
      }
      for(let i in this.infoClaity["engineInfo"]){
        let fcontext=this.infoClaity["engineInfo"][i];
        this.proCla.engineInfo["name"]="工程信息";
        this.proCla.engineInfo["refName"]="工程信息";
        this.proCla.engineInfo[fcontext]=curpro[fcontext];
      }
      for(let i in this.infoClaity["investmentInfo"]){
        let fcontext=this.infoClaity["investmentInfo"][i];
        this.proCla.investmentInfo["name"]="投资信息";
        this.proCla.investmentInfo["refName"]="投资信息";
        this.proCla.investmentInfo[fcontext]=curpro[fcontext];
      }
      for(let i in this.infoClaity["constructionInfo"]){
        let fcontext=this.infoClaity["constructionInfo"][i];
        this.proCla.constructionInfo["name"]="建设信息";
        this.proCla.constructionInfo["refName"]="建设信息";
        this.proCla.constructionInfo[fcontext]=curpro[fcontext];
      }

    },
    // tab click
    jump(tab, event) {
      let target = document.querySelector('.scroll-content')
      let scrollItems = document.querySelectorAll('.scroll-item')
      // 判断滚动条是否滚动到底部
      if (target.scrollHeight <= target.scrollTop + target.clientHeight) {
        this.tabIndex = tab.index.toString()
      }
      let totalY = scrollItems[tab.index].offsetTop - scrollItems[0].offsetTop // 锚点元素距离其offsetParent(这里是body)顶部的距离(待滚动的距离)
      let distance = document.querySelector('.scroll-content').scrollTop // 滚动条距离滚动区域顶部的距离
      // let distance = document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset // 滚动条距离滚动区域顶部的距离(滚动区域为窗口)
      // 滚动动画实现, 使用setTimeout的递归实现平滑滚动，将距离细分为50小段，10ms滚动一次
      // 计算每一小段的距离
      let step = totalY / 10
      if (totalY > distance) {
        smoothDown(document.querySelector('.scroll-content'))
      } else {
        let newTotal = distance - totalY
        step = newTotal / 10
        smoothUp(document.querySelector('.scroll-content'))
      }

      // 参数element为滚动区域
      function smoothDown(element) {
        if (distance < totalY) {
          distance += step
          element.scrollTop = distance
          setTimeout(smoothDown.bind(this, element), 10)
        } else {
          element.scrollTop = totalY
        }
      }

      // 参数element为滚动区域
      function smoothUp(element) {
        if (distance > totalY) {
          distance -= step
          element.scrollTop = distance
          setTimeout(smoothUp.bind(this, element), 10)
        } else {
          element.scrollTop = totalY
        }
      }
    },
    // 滚动条滚动
    onScroll(e) {
      let scrollItems = document.querySelectorAll('.scroll-item')
      for (let i = scrollItems.length - 1; i >= 0; i--) {
        // 判断滚动条滚动距离是否大于当前滚动项可滚动距离
        let judge =
          e.target.scrollTop >=
          scrollItems[i].offsetTop - scrollItems[0].offsetTop - 400
        if (judge) {
          this.tabIndex = i.toString()
          // 找对应的tab-name值
          this.tabName = this.proCla[this.tabIndex].refName
          break
        }
      }
    },
    getHight() {
      this.contentStyleObj.height = window.innerHeight - 190 + 'px'
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep.customer-tab {
  width: 100%;
  height: 50px;
  background-color: #f5f7fa;
  padding: 4px;
}

::v-deep.el-tabs--card > .el-tabs__header {
  border-bottom: none;
  margin: 0;

  .el-tabs__nav {
    width: 100%;
    display: flex;
    justify-content: space-around;
    border: none;

    .el-tabs__item {
      width: 25%;
      text-align: center;
      border: none;
    }

    .is-active {
      border-radius: 4px;
      background-color: #34BD00FF;
      color: #fff;
    }
  }
}

</style>
